$(function () {
    // alert('aa')

    $('.bind_btn').click(function () {
        // alert(123);

        // 获取信息
        let userid = sessionStorage.getItem('id');
        let bankName = $('.bankName option:selected').html();
        let branchName = $('.branchName input').val();
        let cardNumber = $('.cardNumber input').val();
        // console.log(userid, bankName, branchName,cardNumber);

        switch (bankName) {
            case 1:
                bankName = '汇丰银行'
                break;

            case 2:
                bankName = '得不到银行'
                break;

            case 3:
                bankName = '中国银行'
                break;

            case 4:
                bankName = '工商银行'
                break;

            case 5:
                bankName = '吴彦祖'
                break;

            default:
                break;
        }

        $.ajax({
            url: 'http://139.9.177.51:3333/p2p/bindCard',
            type: 'POST',
            data: {
                userid,
                bankName,
                branchName,
                cardNumber
            },
            success: function (res) {
                // console.log(res);
                if (res.code == 200) {
                    alert('绑定成功!')
                    getBankInfo();
                } else {
                    alert('绑定失败!')
                }

            }
        })
    })

    // 页面开启,加载一次
    getBankInfo();

    // 页面渲染
    function getBankInfo() {
        // alert('有信息!')

        let username = localStorage.getItem('data');

        $.ajax({
            url: 'http://139.9.177.51:3333/p2p/cardList',
            type: 'GET',
            data: {
                userid: sessionStorage.getItem('id')
            },
            success: function (res) {
                console.log(res);
                if (res.code == 200) {
                    if (res.data.length != 0) {
                        alert('200~获取成功')

                        // 定义字符串存放绑定的银行卡信息
                        let cardStr = '';
                        let cardidStr = '';

                        // 遍历出所有绑定的银行卡
                        for (let i = 0; i < 6; i++) {
                            // console.log(res.data[i]);

                            // 渲染一:数据拼装
                            cardStr += `
                        <tr>
                            <th scope="row" class="bankName">${res.data[i].bankName}</th>
                            <td class="username">${username}</td>
                            <td class="cardNumber">${res.data[i].cardNumber}</td>
                            <td class="branchName">${res.data[i].branchName}</td>
                        </tr>
                        `

                        // 渲染'充值中心'银行卡列表
                            cardidStr +=`
                            <option value="${res.data[i].id}">${res.data[i].bankName}${res.data[i].cardNumber}(${res.data[i].branchName})</option>
                            `

                        }
                        // 渲染二: 渲染到页面
                        $('.showCard').html(cardStr);
                        // console.log(res.data.length);  //数据太多,长度定为5
                        $('.cardid').html(cardidStr);
                    } else {
                        return
                    }

                } else {
                    alert('500~获取失败')
                }
            }
        })
    }
})